Построение фракталов на Python с numba идея взята отсюда: https://habr.com/ru/post/242221/
import numpy as np
import matplotlib.pyplot as plt
import numba
plt.rcParams["figure.figsize"] = (20,16)
print(np.__version__)
print(numba.__version__)
1.21.2 0.53.0
resox=1024
resoy=1024
@numba.njit
def make_fractal(matrix,iterations=1024,mag_ratio=1.0,c=(0.285+0.01j)):
x0 = 0.0 #Смещение комплексной плоскости
y0 = 0.0
xshift = (-1.5 / mag_ratio) + x0 #Смещение центра комплексной оси в
yshift = (-1.5 / mag_ratio) + y0 #центр изображения
CXmin = -1.5 / mag_ratio #Следующие константы растягивают
CXmax = 1.5 / mag_ratio #комплексную плоскость до размеров resox x resoy
CXshag = (np.abs(CXmin) + np.abs(CXmax)) / resox #Связываем значение одного пикселя и точки на комплексной плоскости
CYmin = -1.5 / mag_ratio
CYmax = 1.5 / mag_ratio
CYshag = (np.abs(CYmin) + np.abs(CYmax)) / resoy
for x in range(resox):
for y in range(resoy):
iterCount = 0 #Обнуляем количество итераций
# Задаем Z начальное значение
z = (x*CXshag+xshift)+1j*(y*CYshag+yshift)
#Обычно за bailout берут 2, но я взял 4, т.к. результат лучше сглаживается
while ((iterCount <= iterations) and (np.abs(z) <= 4.0)):
z = z**2 + c
iterCount = iterCount + 1
# Формула для сглаживания iterCount-log2(ln(|Z|))
iter2 = iterCount - np.log(np.abs(z)) / np.log(2.0)
#Один из способов расскрашивания
matrix[x][y][0] = (iter2 * 7.0%256.0)
matrix[x][y][1] = (iter2 * 14.0%256.0)
matrix[x][y][2] = (iter2 * 2.0%256.0)
return matrix
matrix = np.zeros(shape=(resox,resoy,3),dtype=int)
for i in [10,25,50,100,500]:
matrix = make_fractal(matrix,iterations=i)
plt.imshow(matrix)
plt.show()
matrix = np.zeros(shape=(resox,resoy,3),dtype=int)
for j in np.linspace(0.01,0.005,10):
matrix = make_fractal(matrix,c = 0.285+1j*j)
plt.imshow(matrix)
plt.show()
matrix = np.zeros(shape=(resox,resoy,3),dtype=int)
for mag in np.linspace(1,3,10):
matrix = make_fractal(matrix,mag_ratio=mag)
plt.imshow(matrix)
plt.show()